import tkinter as tk
from tkinter import filedialog
import shutil
import os

selected_directory = None

def select_directory():
    global selected_directory
    directory = filedialog.askdirectory()
    if directory:
        selected_directory = directory
        entry.delete(0, tk.END)
        entry.insert(0, directory)
        status_label.config(text=f"已选择工作目录: {directory}")

def update_selected_directory(event):
    global selected_directory
    selected_directory = entry.get()

def mkdirs():
    jpg_folder = os.path.join(selected_directory, "jpg")
    jpg_selected_folder = os.path.join(selected_directory, "jpg", "精选")
    jpg_trash_folder = os.path.join(selected_directory, "jpg", "废弃")
    nef_folder = os.path.join(selected_directory, "nef")
    nef_selected_folder = os.path.join(selected_directory, "nef", "精选")
    nef_trash_folder = os.path.join(selected_directory, "nef", "废弃")
    correct_folder = os.path.join(selected_directory, "correct")

    if not os.path.exists(jpg_folder):
        os.makedirs(jpg_folder)
    if not os.path.exists(jpg_selected_folder):
        os.makedirs(jpg_selected_folder)    
    if not os.path.exists(jpg_trash_folder):
        os.makedirs(jpg_trash_folder)
    if not os.path.exists(nef_folder):
        os.makedirs(nef_folder)
    if not os.path.exists(nef_selected_folder):
        os.makedirs(nef_selected_folder)
    if not os.path.exists(nef_trash_folder):
        os.makedirs(nef_trash_folder)
    if not os.path.exists(correct_folder):
        os.makedirs(correct_folder)


def copy_files():
    global selected_directory
    if selected_directory:
        jpg_folder = os.path.join(selected_directory, "jpg")
        jpg_selected_folder = os.path.join(selected_directory, "jpg", "精选")
        nef_folder = os.path.join(selected_directory, "nef")
        nef_selected_folder = os.path.join(selected_directory, "nef", "精选")

        # 创建nef下的精选文件夹
        if not os.path.exists(nef_selected_folder):
            os.makedirs(nef_selected_folder)

        # 检查jpg下的精选文件夹是否存在
        if not os.path.exists(jpg_selected_folder):
            status_label.config(text="未找到 'jpg/精选' 文件夹。")
            return
        
        
        # 获取jpg下精选文件夹里所有jpg文件的文件名（不包含扩展名）
        jpg_filenames = [os.path.splitext(filename)[0] for filename in os.listdir(jpg_selected_folder) if filename.lower().endswith('.jpg')]

        # 遍历nef文件夹下的所有nef文件
        for filename in os.listdir(nef_folder):
            if filename.lower().endswith('.nef'):
                nef_filename_without_ext = os.path.splitext(filename)[0]
                if nef_filename_without_ext in jpg_filenames:
                    source_file = os.path.join(nef_folder, filename)
                    destination_file = os.path.join(nef_selected_folder, filename)
                    try:
                        shutil.move(source_file, destination_file)
                        status_label.config(text=f"文件 {filename} 已复制。")
                    except Exception as e:
                        status_label.config(text=f"复制文件 {filename} 时出错: {e}")
    else:
        status_label.config(text="请先选择工作目录。")

# 创建主窗口
root = tk.Tk()
root.title("摄影后期提效工具")

# 添加标题标签，使用微软雅黑字体
title_label = tk.Label(root, text="摄影后期提效工具", font=("Microsoft YaHei", 16, "bold"))
title_label.pack(pady=10)

# 创建一个 Frame 用于放置输入框和选择目录按钮
input_frame = tk.Frame(root)
input_frame.pack(pady=5)

# 创建输入框，使用微软雅黑字体
entry = tk.Entry(input_frame, font=("Microsoft YaHei", 12), width=35)
entry.pack(side=tk.LEFT, padx=5)
entry.bind("<KeyRelease>", update_selected_directory)

# 创建选择目录按钮，使用微软雅黑字体
select_dir_button = tk.Button(input_frame, text="选择工作目录", command=select_directory, font=("Microsoft YaHei", 12))
select_dir_button.pack(side=tk.LEFT, padx=5)

# 创建复制文件按钮，使用微软雅黑字体
copy_button = tk.Button(root, text="初始化工作目录", command=mkdirs, font=("Microsoft YaHei", 12))
copy_button.pack(pady=10)

# 创建复制文件按钮，使用微软雅黑字体
copy_button = tk.Button(root, text="jpg精选同步到nef精选", command=copy_files, font=("Microsoft YaHei", 12))
copy_button.pack(pady=10)

# 创建状态标签，使用微软雅黑字体
status_label = tk.Label(root, text="", font=("Microsoft YaHei", 12))
status_label.pack(pady=10)

# 运行主循环
root.mainloop()
    